package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.function.Consumer;

public class BaseRepository {
    protected static class ListDataConsumer<BeanType> implements Consumer<String> {
        boolean isList;
        private Class<BeanType> beanClass;
        Consumer<List<BeanType>> successCallback;
        Consumer<String> errorCallback;

        public ListDataConsumer(Class<BeanType> beanClass, Consumer<List<BeanType>> successCallback, Consumer<String> errorCallback) {
            this.beanClass = beanClass;
            this.successCallback = successCallback;
            this.errorCallback = errorCallback;
        }

        @Override
        public void accept(String json) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject == null)
            {
                if (errorCallback != null)
                    errorCallback.accept("JSON转换错误");
                return;
            }
            boolean success = jsonObject.getBoolean("success");
            if (success)
            {
                List<BeanType> list = jsonObject.getJSONArray("data").toJavaList(beanClass);
                if (successCallback != null)
                    successCallback.accept(list);
            }
            else
            {
                int statusCode = jsonObject.getInteger("statusCode");
                String message = jsonObject.getString("errMessage");
                if (errorCallback != null)
                    errorCallback.accept(statusCode + ": " + message);
            }
        }
    }


    protected static class BeanDataConsumer<BeanType> implements Consumer<String> {
        private Class<BeanType> beanClass;
        Consumer<BeanType> successCallback;
        Consumer<String> errorCallback;

        public BeanDataConsumer(Class<BeanType> beanClass, Consumer<BeanType> successCallback, Consumer<String> errorCallback) {
            this.beanClass = beanClass;
            this.successCallback = successCallback;
            this.errorCallback = errorCallback;
        }

        @Override
        public void accept(String json) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject == null)
            {
                errorCallback.accept("json转换错误");
                throw new RuntimeException("json转换错误");
            }

            boolean success = jsonObject.getBoolean("success");
            if (success)
            {
                BeanType obj = jsonObject.getObject("data", beanClass);
                if (successCallback != null)
                    successCallback.accept(obj);
            }
            else
            {
                int statusCode = jsonObject.getInteger("status");
                String message = jsonObject.getString("message");
                if (errorCallback != null)
                    errorCallback.accept(statusCode + ": " + message);
            }
        }
    }
}